package com.example.financecollegeorderdish.ui.dialog

import android.content.Context
import com.example.financecollegeorderdish.R
import com.example.financecollegeorderdish.databinding.DialogLoadingBinding
import com.lxj.xpopup.core.CenterPopupView
import com.wuhao.lib_common.utils.LoggerHelper
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.functions.Consumer
import java.util.concurrent.TimeUnit

class LoadingDialog(context: Context, var type: Int) : CenterPopupView(context) {
    val mBind: DialogLoadingBinding by lazy { DialogLoadingBinding.bind(contentView) }

    /**
     * 具体实现的类的布局
     *
     * @return
     */
    override fun getImplLayoutId(): Int = R.layout.dialog_loading
    lateinit var subscribe: Disposable
    override fun onCreate() {
        super.onCreate()
        when (type) {
            1 -> {mBind.lottie.setAnimation(R.raw.loading_2)}
            2 -> {mBind.lottie.setAnimation(R.raw.loading_3)}
            3 -> {mBind.lottie.setAnimation(R.raw.loading_4)}
            else -> {}
        }

        mBind.lottie.playAnimation()
        subscribe = Observable.timer(1000, TimeUnit.MILLISECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe((Consumer { dismiss() }), (Consumer { LoggerHelper.e(it.message) }))
    }

    override fun getPopupWidth(): Int {
        return super.getPopupWidth()
    }

    override fun onDestroy() {
        super.onDestroy()
        if (subscribe != null) {
            if (subscribe.isDisposed) {
                subscribe.dispose()
            }
        }
    }
}